<template>
  <div class="goods_container">
    <Breadcrumb menu="商品管理" submenu="商品列表" />

    <el-card>
      <!-- 顶部搜索框 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入商品名称"
            v-model.trim="queryInfo.query"
            @clear="getGoodsList"
            @keyup.enter.native="getGoodsList"
            clearable
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="$router.push('/goods/add')"
            >添加商品</el-button
          >
        </el-col>
      </el-row>
      <!-- 商品列表 -->
      <el-table :data="goodsList" border="">
        <el-table-column prop="goods_id" label="id" width="80px">
        </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="120px">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="90px">
        </el-table-column>
        <el-table-column prop="upd_time" label="创建时间" width="170px">
          <template v-slot="{ row }">
            {{ row.upd_time | formatDate }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template v-slot="{ row }">
            <!-- 编辑按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              round
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              round
              @click="deleteGoodsById(row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 底部分页栏目 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "Goods",
  data() {
    return {
      //商品查询对象
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      //商品列表数据
      goodsList: [],
      total: 0,
    };
  },
  methods: {
    //获取商品数据
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo,
      });
      this.goodsList = res.data.goods;
      this.total = res.data.total;
    },
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getGoodsList();
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getGoodsList();
    },
    //通过id删除商品
    deleteGoodsById(row) {
      this.$confirm(
        `此操作将永久删除商品, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(async () => {
          await this.$http.delete(`goods/${row.goods_id}`);
          this.$message.success("删除成功");
          this.getGoodsList();
        })
        .catch((error) => error);
    },
  },
  created() {
    this.getGoodsList();
  },
};
</script>

<style lang="less" scoped>
.goods_container {
  overflow: auto;
}
</style>